import axios from 'axios'

// 创建axios实例，使用相对路径利用Vite代理
const service = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
})

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    console.log('发送请求:', config.method?.toUpperCase(), config.url)
    return config
  },
  (error) => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    console.log('收到响应:', response.status, response.config.url)
    return response
  },
  (error) => {
    console.error('响应错误:', error.response?.status, error.message)
    return Promise.reject(error)
  }
)

// 用户接口定义
export const userAPI = {
  getList: () => service.get('/user/list'),
  getById: (id: number) => service.get(`/user/${id}`),
  save: (user: any) => service.post('/user/save', user),
  update: (user: any) => service.put('/user/update', user),
  delete: (id: number) => service.delete(`/user/${id}`),
}
